﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
using System.Drawing;
namespace School.Pages.Bus
{
    public partial class Discount1 : System.Web.UI.Page
    {
        private SchoolEntities db = new SchoolEntities();
        IEnumerable<SRV_Bus_Price_Discount> query = School.Service.Bus.GetPrice_Discount();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1205) == true)
                {
                    if (!IsPostBack)
                        txtbind();
                    lbltestBus.Visible = false;
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        private void txtbind()
        {
            txtpath.Text = query.First().Discount.ToString();
            txtterm.Text = query.Single(x => x.Feature == "one_Term").Discount.ToString();
            txtpathterm.Text = query.Last().Discount.ToString();
        }
        protected void btnsave_Click(object sender, EventArgs e)
        {
            int Idpath = query.First().Id;
            int Idpathterm = query.Last().Id;
            int Idtrem = query.Single(x => x.Feature == "one_Term").Id;
            if (string.IsNullOrEmpty(txtpath.Text))
                txtpath.Text = "0";
            if (string.IsNullOrEmpty(txtterm.Text))
                txtterm.Text = "0";
            if (string.IsNullOrEmpty(txtpathterm.Text))
                txtpathterm.Text = "0";

            bool re = School.Service.Bus.UpdateDiscount(Idpath, (float.Parse)(txtpath.Text));
            re = School.Service.Bus.UpdateDiscount(Idtrem, float.Parse(txtterm.Text));
            re = School.Service.Bus.UpdateDiscount(Idpathterm, float.Parse(txtpathterm.Text));
            if (re == true)
            {
                lbltestBus.ForeColor = Color.Green;
                lbltestBus.Text = "تم التحديث بنجاح";
                txtbind();
            }
            else
            {
                lbltestBus.ForeColor = Color.Red;
                lbltestBus.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lbltestBus.Visible = true;
        }
    }
}